package com.etc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.dao.MusicMapper;
import com.etc.dao.TalkMapper;
import com.etc.dao.UserMapper;
import com.etc.entity.Music;
import com.etc.entity.MusicExample;
import com.etc.entity.Talk;
import com.etc.entity.TalkExample;
import com.etc.entity.TalkExample.Criteria;
import com.etc.entity.User;
import com.etc.service.MusicService;
import com.etc.utils.TalkExtent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class MusicServiceImpl implements MusicService {
	@Autowired
	private MusicMapper musicMapper;

	@Autowired

	private TalkMapper talkMapper;

	@Autowired

	private UserMapper userMapper;

	@Override
	public PageInfo<Music> pageMusic(Integer pageSize, Integer pageNum,String mstatus) {
		PageHelper.startPage(pageNum, pageSize);
		MusicExample example = new MusicExample();
		if(mstatus.equals("yes")){
			com.etc.entity.MusicExample.Criteria createCriteria = example.createCriteria();
			createCriteria.andMAidEqualTo(1);
		}else if(mstatus.equals("no")){
			com.etc.entity.MusicExample.Criteria createCriteria = example.createCriteria();
			createCriteria.andMAidEqualTo(0);
		}
		List<Music> musics = musicMapper.selectByExample(example);
		PageInfo<Music> musicInfo = new PageInfo<>(musics);
		return musicInfo;
	}

	@Override
	public Integer addMusic(Music music) {
		int i = musicMapper.insert(music);

		return i;
	}

	@Override
	public List<Music> findAll() {
		MusicExample example = new MusicExample();
		List<Music> list = musicMapper.selectByExample(example);
		return list;
	}

	@Override
	public Music selectById(Integer mid) {
		Music music = musicMapper.selectByPrimaryKey(mid);
		return music;
	}

	@Override
	public List<TalkExtent> selecttalkMassage(Integer mid, String tcategory, Integer ttopicid) {
		List<Talk> list = talkMapper.selecttalkMassage(mid, tcategory, ttopicid);
		List<TalkExtent> talklist = new ArrayList<>();
		for (Talk talk : list) {
			TalkExtent talkExtent = new TalkExtent();
			int count = this.selectchildCount(talk);
			talkExtent.setTalk(talk);
			talkExtent.setChildCount(count);
			talklist.add(talkExtent);
		}
		return talklist;
	}

	public int selectchildCount(Talk talk) {
		TalkExample example = new TalkExample();
		Criteria criteria = example.createCriteria();
		criteria.andTtopicidEqualTo(talk.getTid());
		List<Talk> list = talkMapper.selectByExample(example);
		return list.size();
	}

	@Override
	public int selectCount(Integer mid, String tcategory) {
		TalkExample example = new TalkExample();
		Criteria criteria = example.createCriteria();
		criteria.andTcategoryEqualTo(tcategory);
		criteria.andTarticleidEqualTo(mid);
		List<Talk> list = talkMapper.selectByExample(example);
		int count = list.size();
		return count;
	}

	@Override
	public Talk addMusicTalk(Talk talk) {
		talk.setTcreatetime(new Date());
		talkMapper.insertMusicTalk(talk);
		User user = userMapper.selectByPrimaryKey(talk.gettUid());
		talk.setUser(user);
		return talk;
	}

	@Override
	public PageInfo<Talk> selecttalkMassages(Integer mid, String tcategory, Integer ttopicid, Integer pageSize,
			Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<Talk> list = talkMapper.selecttalkMassage(mid, tcategory, ttopicid);
		PageInfo<Talk> talkInfo = new PageInfo<>(list);
		return talkInfo;
	}

	@Override
	public PageInfo<Music> selectByNameOrTitle(Integer pageSize, Integer pageNum, String mstatus, String info) {
		PageHelper.startPage(pageNum, pageSize);
		Integer mAid=0;
		if(info.isEmpty()){
			MusicExample example=new MusicExample();
			List<Music> musics = musicMapper.selectByExample(example);
			PageInfo<Music> musicInfo = new PageInfo<>(musics);
			return musicInfo;
		}
		if(mstatus.equals("yes")){
			mAid=1;
			List<Music> musics = musicMapper.selectByNameOrTitle(mAid,info);
			PageInfo<Music> musicInfo = new PageInfo<>(musics);
			return musicInfo;
		}else if(mstatus.equals("all")){
			
			List<Music> musics = musicMapper.selectByNameOrTitleall(info,info);
			PageInfo<Music> musicInfo = new PageInfo<>(musics);
			return musicInfo;
		}else{
			List<Music> musics = musicMapper.selectByNameOrTitle(mAid,info);
			PageInfo<Music> musicInfo = new PageInfo<>(musics);
			return musicInfo;
		}
	}

	
	@Override
	public String deletemusic(Integer mid) {
		int i = musicMapper.deleteByPrimaryKey(mid);
		if(i>0){
			return "删除成功";
		}
		else {
			return "删除失败";
		}
		
	}

	@Override
	public void updatemAid(Integer mAid, Integer mid) {
		Music music = musicMapper.selectByPrimaryKey(mid);
		if(mAid==1){
			music.setmAid(0);
		}else{
			music.setmAid(1);
		}
		musicMapper.updateByPrimaryKey(music);
		
	}
		
		


}

